//
//  RKTabBarController.swift
//  RKWeibo
//
//  Created by Rick on 2017/3/2.
//  Copyright © 2017年 Ricky. All rights reserved.
//

import UIKit

class RKTabBarController: UITabBarController {
    //Lazy is only valid for members of a struct or class
    fileprivate var composeButton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
//    composeButton.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
//    composeButton.setBackgroundImage(UIImage.init(named:"tabbar_compose_button_highlighted"), for: .highlighted)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 'setupChildControllers' is inaccessible due to 'private' protection level
        setupChildControllers()
        setupComposeButton()
        
    }

     @objc fileprivate func composetButtonClick()->(){
       print("hahahahah")
       let controller = UIViewController()
        controller.view.backgroundColor = UIColor.cz_random()
        let uNC = UINavigationController(rootViewController: controller)
        present(uNC, animated: true, completion: nil)
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
       return .portrait
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
}

extension RKTabBarController{
    fileprivate func setupComposeButton(){
        tabBar.addSubview(composeButton)
        let w = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1.0
        composeButton.frame = tabBar.bounds.insetBy(dx: 2*w, dy: 0)
        // 'composetButtonClick' is inaccessible due to 'private' protection level
        composeButton.addTarget(self, action: #selector(composetButtonClick), for: .touchUpInside)
    }
    
    fileprivate func setupChildControllers(){
        let array = [
            ["clsname":"RKHomeController","title":"首页","imagename":"home"],
            ["clsname":"RKMessageController","title":"消息","imagename":"message_center"],
            ["clsname":"UIViewController"],
            ["clsname":"RKDiscoverController","title":"发现","imagename":"discover"],
            ["clsname":"RKProfileController","title":"我","imagename":"profile"]
        ]
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict: dict))
        }
        viewControllers = arrayM
    }
    
    /// 使用字典创建一个控制器
    ///
    /// - Parameter dict: 字典格式[clsname,title,imagename]
    /// - Returns:控制器
    private func controller(dict:[String:String])->UIViewController{
        guard let clsname = dict["clsname"],
              let title = dict["title"],
              let imagename = dict["imagename"],
              let cls = NSClassFromString(Bundle.main.namespece + "." + clsname) as? UIViewController.Type
        else {
            return UIViewController()
        }
        let controller = cls.init()
        //设置标题图像
        controller.title = title;
        controller.tabBarItem.image = UIImage(named: "tabbar_"+imagename)
        controller.tabBarItem.selectedImage = UIImage(named: "tabbar_"+imagename+"_selected")?.withRenderingMode(.alwaysOriginal)
        
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.black], for: .highlighted)
        controller.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12)], for: .normal)
        
        let uNC = RKNavigationController(rootViewController: controller)
        return uNC
    }

}
